package org.nullauthor.allthings.block.entity;

import net.minecraft.block.BlockState;
import net.minecraft.block.entity.BlockEntity;
import net.minecraft.block.entity.BlockEntityType;
import net.minecraft.block.entity.LootableContainerBlockEntity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.entity.player.PlayerInventory;
import net.minecraft.inventory.Inventories;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NbtCompound;
import net.minecraft.screen.*;
import net.minecraft.text.Text;
import net.minecraft.util.collection.DefaultedList;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
import org.nullauthor.allthings.gui.handler.EffectUpgradeScreenHandler;
import org.nullauthor.allthings.gui.inventory.DefaultInventory;
import org.nullauthor.allthings.gui.screen.EffectUpgradeScreen;
import org.nullauthor.allthings.mods.ModBlockEntityTypes;

public class EffectUpgradeEntity extends BlockEntity implements NamedScreenHandlerFactory, DefaultInventory {
    private DefaultedList<ItemStack> inv = createInventory();

    public EffectUpgradeEntity(BlockPos pos, BlockState state) {
        super(ModBlockEntityTypes.EFFECT_UPGRADE_ENTITY, pos, state);
    }

    private DefaultedList<ItemStack> createInventory() {
        return DefaultedList.ofSize(getInventorySize(), ItemStack.EMPTY);
    }

    @Override
    public ScreenHandler createMenu(int syncId, PlayerInventory playerInventory, PlayerEntity player){
        return new EffectUpgradeScreenHandler(syncId, playerInventory);
    }

    @Override
    public Text getDisplayName() {
        return Text.translatable("container.effect_upgrade_container");
    }

    /**
     * 获取本方块自身物品栏的容量
     *
     * @return 物品栏容量，用于构造或初始化物品栏
     */
    public static int getInventorySize() {
        return 4;
    }

    @Override
    public DefaultedList<ItemStack> getInventory() {
        return inv;
    }
    /**
     * 从方块实体的NBT数据中读取该方块物品栏中保存的物品<br/>
     * 最大的用途是在进入世界时能够加载上次退出世界前该方块内保存的内容<br/>
     * 这样方块里面的东西就可以保存下来，而不会丢失了<br/>
     * 我们也把工作进度读取一下
     *
     * @param nbt 方块实体的NBT
     */
    @Override
    public void readNbt(NbtCompound nbt) {
        Inventories.readNbt(nbt, this.getInventory());
    }

    /**
     * 将该方块物品栏中保存的物品写入到该方块的NBT数据中<br/>
     * 最大的用途是在退出世界时能够保存本次退出世界前该方块内保存的内容<br/>
     * 这样方块里面的东西就可以保存下来，而不会丢失了<br/>
     * 我们也把工作进度保存一下
     *
     * @param nbt 方块实体的NBT
     */
    @Override
    protected void writeNbt(NbtCompound nbt) {
        Inventories.writeNbt(nbt, this.getInventory());
    }
}
